package com.linjuke.mayouting.biz.dal.ibatis;

import java.util.List;

import com.linjuke.mayouting.biz.ao.query.UserQuery;
import com.linjuke.mayouting.biz.dal.daointerface.UserDAO;
import com.linjuke.mayouting.biz.dal.dataobject.UserDO;
import com.zjuh.sweet.lang.CollectionUtil;

public class UserDAOIbatis extends BaseIbatisDAO implements UserDAO {

	@Override
	public int create(UserDO user) {
		return (Integer)this.getSqlMapClientTemplate().insert("UserDAO.create", user);
	}

	@Override
	public int delete(int id) {
		return this.getSqlMapClientTemplate().update("UserDAO.delete", id);
	}

	@Override
	public UserDO queryById(int id) {
		return (UserDO)this.getSqlMapClientTemplate().queryForObject("UserDAO.queryById", id);
	}

	@Override
	public int update(UserDO user) {
		return this.getSqlMapClientTemplate().update("UserDAO.update", user);
	}

	@Override
	public UserDO queryByUsername(String username) {
		return (UserDO)this.getSqlMapClientTemplate().queryForObject("UserDAO.queryByUsername", username);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserDO> queryUsersForPage(UserQuery userQuery) {
		int count = (Integer)this.getSqlMapClientTemplate().queryForObject("UserDAO.queryUsersForPageCount", userQuery);
		List<UserDO> users = (List<UserDO>)this.getSqlMapClientTemplate().queryForList("UserDAO.queryUsersForPage", userQuery);
		userQuery.setTotalResultCount(count);
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserDO> queryByIds(List<Integer> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return CollectionUtil.newArrayList();
		}
		return (List<UserDO>)this.getSqlMapClientTemplate().queryForList("UserDAO.queryByIds", ids);
	}

}
